<template>
  <div class="app-container">
    <el-button type="primary" @click="centerDialogVisible = true" icon="el-icon-plus" size="mini" style="margin-bottom: 10px;">添加一个剧本</el-button><br>

    <el-table v-loading="listLoading" :data="list" border fit highlight-current-row style="width: 100%">
      <el-table-column align="center" label="ID" width="80">
        <template slot-scope="{row}">
          <span>{{ row.id }}</span>
        </template>
      </el-table-column>

      <el-table-column width="360px" align="center" label="nickName">
        <template slot-scope="{row}">
          <span>{{ row.nickName }}</span>
        </template>
      </el-table-column>



      <el-table-column width="200px" label="playbook">
        <template slot-scope="{row}">
          <span>{{ row.playbook }}</span>
        </template>
      </el-table-column>
            <el-table-column width="180px" align="center" label="描述">
        <template slot-scope="{row}">
          <span>{{ row.funcName }}</span>
        </template>
      </el-table-column>
      <el-table-column width="120px" label="编辑">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="showContent(row)">查看内容</el-button>

        </template>
      </el-table-column>
      <!-- <el-table-column width="100px" label="执行">
        <template slot-scope="{row}">
          <el-button type="primary" size="mini" @click="showContent(row)">执行</el-button>
        </template>
      </el-table-column> -->
    </el-table>

    <el-dialog
      title="添加一个 PlayBook"
      :visible.sync="centerDialogVisible"
      width="60%"
      center>

        <el-row class="demo-autocomplete">
          <el-col :span="12">
              <div class="demo-input-suffix" style="padding-bottom: 5px">
                描述PlayBook功能
                </div>
                <el-input v-model="new_playbook_desp" style="width:98%"></el-input>
              
          </el-col>
          <el-col :span="12">
              <div class="demo-input-suffix" style="padding-bottom: 5px;">
                PlayBook 文件名
              </div>
                <el-input v-model="new_playbook_filename" style="width:98%"></el-input>
          </el-col>
        </el-row><br>
        <div class="demo-input-suffix" style="padding-bottom: 5px;">
          PlayBook 内容
        </div>

        <yaml-editor  v-model="playbook_content" />

      <span slot="footer" class="dialog-footer">
        <el-button plain size="small" @click="centerDialogVisible = false">取 消</el-button>
        <el-button type="primary" plain size="small"  @click="submit_playbook">添 加</el-button>
      </span>
    </el-dialog>


  </div>
</template>

<script>
import Axios from 'axios'

import { fetchList, getApi } from '@/api/ansibleui'
import YamlEditor from '@/components/yamlEditor'
const playbook_content = "- hosts: all\n  become: yes\n  become_method: sudo\n  gather_facts: no\n\n  tasks:\n  - name: \"install {{ package_name }}\"\n    package:\n      name: \"{{ package_name }}\"\n      state: \"{{ state | default('present') }}\"";

export default {
  name: 'InlineEditTable',
  components: { YamlEditor },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: 'success',
        draft: 'info',
        deleted: 'danger'
      }
      return statusMap[status]
    }
  },
  data() {
    return {
      new_playbook_desp: '',
      new_playbook_filename: '',
      playbook_content: playbook_content,
      centerDialogVisible: false,
      list: null,
      listLoading: true,
      listQuery: {
        page: 1,
        limit: 10
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    submit_playbook(){
      
    },
    async getList() {
      this.listLoading = true
      const { data } = await fetchList('playbooks', this.listQuery)
      const items = data.items
      this.list = items.map(v => {
        this.$set(v, 'edit', false) // https://vuejs.org/v2/guide/reactivity.html
        v.originalTitle = v.title //  will be used when user click the cancel botton
        return v
      })
      this.listLoading = false
    },
    cancelEdit(row) {
      row.title = row.originalTitle
      row.edit = false
      this.$message({
        message: 'The title has been restored to the original value',
        type: 'warning'
      })
    },
    showContent(row) {
      console.log(row)
      this.playbook_content = ""
      Axios.get(this.$root.baseUrl+'/playbook/content/'+ row.id +'/').then(resp => {
      this.playbook_content = resp.data.content
      this.centerDialogVisible = true
        })
      // var data = getApi('/')
    }
  }
}
</script>

<style scoped>
.edit-input {
  padding-right: 100px;
}
.cancel-btn {
  position: absolute;
  right: 15px;
  top: 10px;
}
</style>
